package com.application.metroscanner.fragments;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYSeries;
import com.application.metroscanner.GraphicsActivity;
import com.application.metroscanner.Ride;
import com.application.metroscanner.tools.Classifier;
import com.example.metroscanner.R;

public class RideGroupGraphicFragment extends Fragment {

	Classifier.Cluster cluster;
	private XYPlot plot;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_groups_graphics,
				container, false);
		
		Bundle bundle = getArguments();
		Classifier.Cluster cluster = (Classifier.Cluster) bundle
				.get(GraphicsActivity.SELECTED_CLUSTER);

		DisplayGraphic(ProcessCluster(cluster), view);

		return view;
	}

	private XYSeries ProcessCluster(Classifier.Cluster iCluster) {
		SparseArray<Point> PointsByStartTime = new SparseArray<Point>();
		ArrayList<Ride> rides = iCluster.getRides();
		// create points from rides data
		for (int r = 0; r < rides.size(); ++r) {
			Ride ride = rides.get(r);
			int startTime = ride.getHours() * 60 + ride.getMinutes();
			Point point = PointsByStartTime.get(startTime);
			if (point != null) {
				point.AddRide(ride);
			} else {
				PointsByStartTime.put(startTime, new Point(ride));
			}
		}

		// create serie
		ArrayList<Integer> xList = new ArrayList<Integer>();
		ArrayList<Float> yList = new ArrayList<Float>();

		for (int p = 0; p < PointsByStartTime.size(); ++p) {
			Point point = PointsByStartTime.valueAt(p);
			xList.add(point.startTime);
			yList.add(point.meanTime / 60000 );
		}

		XYSeries serie = new SimpleXYSeries(xList, yList, "Ride Time");
		return serie;
	}

	private void DisplayGraphic(XYSeries iSerie, View iView) {
		// initialize our XYPlot reference:
		plot = (XYPlot) iView.findViewById(R.id.mySimpleXYPlot);
		// Create a formatter to use for drawing a series using
		// LineAndPointRenderer
		// and configure it from xml:
		LineAndPointFormatter seriesFormat = new LineAndPointFormatter(
				Color.RED, Color.GREEN, Color.BLUE, null);

		plot.addSeries(iSerie, seriesFormat);

		// reduce the number of range labels
		plot.setTicksPerRangeLabel(3);
		plot.getGraphWidget().setDomainLabelOrientation(-45);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onDetach() {
		super.onDetach();
	}

	private class Point {
		private int startTime = 0;
		private int nbRides = 0;
		private float meanTime = 0;
		private float meanRating = 0;

		public Point(Ride iRide) {
			startTime = iRide.getHours() * 60 + iRide.getMinutes();
			nbRides = 1;
			meanTime = iRide.getRideTime();
			meanRating = (float) iRide.getRating();
		}

		public boolean AddRide(Ride iRide) {
			boolean res = false;
			if (iRide.getHours() * 60 + iRide.getMinutes() == startTime) {
				nbRides++;
				meanTime = (meanTime * (nbRides - 1) + iRide.getRideTime())
						/ nbRides;
				meanRating = (meanRating * (nbRides - 1) + iRide.getRating())
						/ nbRides;
				res = true;
			}
			return res;
		}
	}

}
